﻿using System;
using Telerik.Web.UI;
using WorkOrder.Business;

namespace WorkOrder.Notice
{
    public partial class ManageNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
            else if (!Page.IsPostBack)
                RefreshData("");
        }

        private void RefreshData(string srtWhere)
        {
            #region 查询数据
            Business_Table_Notice B_T_N = new Business_Table_Notice();
            RadListView1.DataSource = B_T_N.GetList(srtWhere);
            RadListView1.DataBind();
            #endregion
        }

        protected void RadListView1_PageIndexChanged(object sender, RadListViewPageChangedEventArgs e)
        {
            RadListView1.CurrentPageIndex = e.NewPageIndex;
            if (txtSearch.Text.Trim() != "")
            {
                RefreshData("(B.TypeName like '%" + txtSearch.Text.Trim() + "%' or A.Title like '%" + txtSearch.Text.Trim() +
                            "%' or C.remark like '%" + txtSearch.Text.Trim() + "%')");
            }
            else
            {
                RefreshData("");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim() != "")
            {
                RefreshData("(B.TypeName like '%" + txtSearch.Text.Trim() + "%' or A.Title like '%" + txtSearch.Text.Trim() +
                            "%' or C.remark like '%" + txtSearch.Text.Trim() + "%')");
            }
            else
            {
                RefreshData("");
            }
        }

        protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                if (new Business_Table_Permission().HasDelete(Session["UserID"].ToString(), "Table_Notice"))
                {
                    new Business_Table_Notice().Delete(int.Parse(e.CommandArgument.ToString()));
                    if (txtSearch.Text.Trim() != "")
                    {
                        RefreshData("(B.TypeName like '%" + txtSearch.Text.Trim() + "%' or A.Title like '%" + txtSearch.Text.Trim() +
                                    "%' or C.remark like '%" + txtSearch.Text.Trim() + "%')");
                    }
                    else
                    {
                        RefreshData("");
                    }
                    RadAjaxPanel1.ResponseScripts.Add("alert('公告删除成功！')");

                }
                else
                    RadAjaxPanel1.ResponseScripts.Add("alert('对不起，您没有删除公告的权限！')");
            }
        }

    }
}